(function ($) {


	$.fn.exists = function()
	{
		return $(this).length>0;
	};
	
	
	/*--------------------------------------------------------------------------------------------
		setup_forms
	--------------------------------------------------------------------------------------------*/
	function setup_forms()
	{
		$('form').each(function(){
		
			var form = $(this);
			
			form.validate({
				highlight: function(element, errorClass, validClass) {
				    $(element).closest('.field').addClass('error');
				},
				unhighlight: function(element, errorClass, validClass) {
				    $(element).closest('.field').removeClass('error');
				}
			});
		});
	}
	
	
	/*--------------------------------------------------------------------------------------------
	*	
	*    Setup Nav
	*
	*--------------------------------------------------------------------------------------------*/
	
	function setup_nav()
	{
		// elements
		var nav = $('#nav');
		var nav_ul = $('#nav ul');
		var fake_nav = $('#fake_nav');
		var nav_orig_y = nav.position().top;

		// vars
		var window_y = 0;
		var nav_ul_visible = false;
		
		nav.find('h3').stop().css({'opacity' : '0'});
		
		$(window).scroll(function(){
			
			window_y = $(window).scrollTop();
			
			// fake nav
			if(window_y >= (nav_orig_y - 40))
			{
				
				if(!nav_ul_visible)
				{
					nav_ul_visible = true;
					
					nav.find('h2').animate({'margin-left' : '95px', 'opacity' : 0.5}, 500, function(){
						if(nav_ul_visible)
						{
							nav.find('h3').animate({'opacity' : '1'}, 500);
						}
					});
					
					fake_nav.css({'display' : 'none'});
					nav_ul.animate({'opacity' : 1}, 300);
				}
			}
			else if(window_y < (nav_orig_y - 40))
			{
				
				if(nav_ul_visible)
				{
					nav_ul_visible = false;
					
					nav.find('h3').stop().animate({'opacity' : '0'}, 300);
					nav.find('h2').animate({'margin-left' : '0px', 'opacity' : 1}, 500);
					
					fake_nav.css({'display' : 'block'});
					nav_ul.animate({'opacity' : 0}, 300);
				}
			}
			
			// nav
			if(window_y >= nav_orig_y)
			{
				nav.css({'position' : 'fixed', 'top' : 0});
			}
			else if(window_y < nav_orig_y)
			{
				nav.css({'position' : 'absolute', 'top' : 'auto'});
			}
			
			// nav ul
			/*if(window_y < (nav_orig_y - 36))
			{
				nav_ul.css({'margin-top' : '-' + ((nav_orig_y - 36) - window_y) + 'px'});
			}*/
			
		
		}).trigger('scroll');
		
		$('.nav a').click(function(){
			
			var id = $(this).attr('data-rel');
			if(!$(id).exists()){
				return true;
			}
			var scroll_y = $(id).offset().top;
			var minus = (id == '#folio') ? 50 : 35;
			
			if($('#folio_single').height() > 0)
			{
				$('#folio_close').trigger('click');
				setTimeout(function(){
					scroll_y = $(id).offset().top;
					$('body, html').stop().animate({'scrollTop' : scroll_y - minus}, 500);
				}, 500);
			}
			else
			{
				$('body, html').stop().animate({'scrollTop' : scroll_y - minus}, 500);
			}
			
			
			
			return false;
			
		});
	}
	
	
	/*--------------------------------------------------------------------------------------------
	*	
	*    setup_services
	*
	*--------------------------------------------------------------------------------------------*/
	
	function setup_services()
	{
		var service = $('#services');
		var is_animated = false;
		var mouse_out = false;
		
		if($.browser.msie)
		{
			return false;
		}
		
		service.find('.service').each(function(){
		
			$(this).hover(function(){
				
				$(this).find('.circle').animate({
					top : '5px',
					left : '5px',
					width : '310px',
					height : '310px'
				}, 250);
				
				$(this).find('.circle .inner').animate({
					top : '-5px',
					left : '-5px',
					'background-color' : '#d0d0d0'
				}, 250);

				
			}, function(){
				
				
				
				$(this).find('.circle').animate({
					top : '0px',
					left : '0px',
					width : '320px',
					height : '320px'
				}, 250);
				
				$(this).find('.circle .inner').animate({
					top : '0px',
					left : '0px',
					'background-color' : '#fff'
				}, 250);
				
								
			});
			
		});
		
		// rocket
		service.find('.service.last').hover(function(){
		
			mouse_out = false;
			
			$(this).find('.icon.deliver').each(function(){
					
				var rocket = $(this);
				
				if(!is_animated)
				{						
					is_animated = true;
					$(this).removeClass('fall_down').addClass('lift_off');
					setTimeout(function(){
						is_animated = false;
						if(mouse_out)
						{
							is_animated = true;
							rocket.removeClass('lift_off').addClass('fall_down');
							setTimeout(function(){
								is_animated = false;
								rocket.removeClass('fall_down');
							}, 1000);
						}
						else
						{
							rocket.addClass('in_sky').removeClass('lift_off');
						}
						
					}, 3000);
				}
				
			});
		
		}, function(){
			
			mouse_out = true;
			
			$(this).find('.icon.deliver').each(function(){
				
				var rocket = $(this);

				if(!is_animated && rocket.hasClass('in_sky'))
				{
					is_animated = true;
					rocket.removeClass('in_sky').addClass('fall_down');
					setTimeout(function(){
						is_animated = false;
						rocket.removeClass('fall_down');
					}, 1000);
				}

				
			});

		});
	}
	
	
	/*--------------------------------------------------------------------------------------------
	*	
	*    Setup Folio
	*
	*--------------------------------------------------------------------------------------------*/
	
	function setup_folio()
	{
		// elements
		var folio = $('#folio');
		var folio_single = $('#folio_single');
		var folio_nav = $('#folio_nav');
		var ajax_response = $('#ajax_response');
		var folio_projects = $('#folio_projects');
		var temp_project = $('#temp_project');
		
		// vars
		var active = false;
		var scroll_nav = true;
		var project_1 = false;
		var project_2 = false;
		
		//events
		folio_projects.find('.project').hover(function(){
			$(this).find('img').animate({opacity : 0}, 250);
		}, function(){
			$(this).find('img').animate({opacity : 1}, 250);
		});
		
		// if no nav, it is a single page via google. No ajax stuff please.
		if(!folio_nav.exists())
		{
			return false;
		}
		
		folio_projects.find('.project a').click(function(){
			
			$(this).parent().addClass('active');
			
			var ajax_url = $(this).attr('href').replace('/projects/', '/projects-ajax/');
			
			var data = {
				url : ajax_url
			};
			
			load_project(data);
			
			return false;
			
		});
		
		// load project 
		function load_project(options)
		{
			
			// default options
			var defaults = {
				direction	:	'left',
				url : "/folio-ajax/"
			};
			
			// default options can be overridden by incoming options
			options  = $.extend(defaults, options);
			
			$.ajax({
				url: options.url,
				context: document.body,
				success: function(data){
					
					// remove loading from button
					$('#folio_next, #folio_prev').removeClass('loading');
					
					// new height
					temp_project.html(data);
					var call_limit = 0;
					
					function slide_in_content(){
						
						call_limit++;
						
						$('body, html').animate({'scrollTop' : folio_single.position().top - 50}, 500);
							
							if(temp_project.height() < 100)
							{
								// something has gone wrong!
								if(call_limit < 10)
								{
									setTimeout(function(){
								
										slide_in_content();
									
									}, 100); 
								}
								return false;
							}
							
							var new_height = temp_project.height() + 70; // +70 is the (inside) container padding of 35 + 35
							new_height = temp_project.height() + 70;
							new_height = temp_project.height() + 70;
							
							folio_single.animate({'height' : new_height}, 500);
							
							if(active)
							{
								project_1 = project_2;
								
								project_2 = false;
	
								project_1.addClass('last_active');
								
								project_2 = $('<div class="project ' + options.direction + '"></div>');
								project_2.html(data);
								
								ajax_response.append(project_2);
								project_2.animate({'left' : '0%'}, 750, function(){
									project_1.remove();								
								});
														
							}
							else
							{
								ajax_response.find('.project').remove();
								
								project_2 = $('<div class="project"></div>');
								project_2.html(data);
								
								ajax_response.append(project_2);
								project_2.css({'left' : '0%'});
									
								$('.section_mask').animate({'height' : 'toggle'}, 500);
							}
							
							active = true;
					}
					
					setTimeout(function(){
					
						slide_in_content();
					
					}, 100); 
					
							
				}
			});
			
			
		}
		
		// folio_close
		folio_nav.find('a#folio_close').click(function(){
			
			folio_projects.find('.project.active').removeClass('active');
			
			$('body, html').animate({'scrollTop' : folio.position().top - 50}, 500);
			
			close_project();
			
			return false;
			
		}).hover(function(){
				
			$(this).find('.outer').animate({
				marginTop : '-40px',
				marginLeft : '-40px',
				width : '81px',
				height : '81px'
			}, 250);
			
		}, function(){
			
			$(this).find('.outer').animate({
				marginTop : '-35px',
				marginLeft : '-35px',
				width : '71px',
				height : '71px'
			}, 250);
			
		});
				
		// close project
		function close_project()
		{

			//folio_nav.animate({'margin-top' : '-35px'}, 500);
			folio_single.animate({'height' : 0}, 500, function(){
				active = false;
			});
			
			// hide all other sections
			$('.section_mask').animate({'height' : 'toggle'}, 500);
			
		}
		
		
		// folio_prev
		folio_nav.find('a#folio_prev').click(function(){
			
			// set loading
			$(this).addClass('loading');
			
			// find current project
			var active = folio_projects.find('.project.active');
			if (active.length == 0) active = folio_projects.find('.project:first');
			
			// find next project
			var prev =  active.prev('.project').length ? active.prev('.project') : folio_projects.find('.project:last');

        	// remove active
        	active.removeClass('active');
        	
        	// add active
        	prev.addClass('active');
			
			var ajax_url = prev.children('a').attr('href').replace('/projects/', '/projects-ajax/');
			
			var data = {
				url : ajax_url,
				direction : 'right'
			};
			
			load_project(data);
			
			return false;
			
		}).hover(function(){
				
			$(this).find('.outer').animate({
				marginTop : '-32px',
				marginLeft : '-32px',
				width : '65px',
				height : '65px'
			}, 250);
			
		}, function(){
			
			$(this).find('.outer').animate({
				marginTop : '-35px',
				marginLeft : '-35px',
				width : '71px',
				height : '71px'
			}, 250);
			
		});
		
		// folio_next
		folio_nav.find('a#folio_next').click(function(){
			
			// set loading
			$(this).addClass('loading');
			
			// find current project
			var active = folio_projects.find('.project.active');
			if (active.length == 0) active = folio_projects.find('.project:last');
			
			// find next project
			var next =  active.next('.project').length ? active.next('.project') : folio_projects.find('.project:first');

        	// remove active
        	active.removeClass('active');
        	
        	// add active
        	next.addClass('active');
			
			var ajax_url = next.children('a').attr('href').replace('/projects/', '/projects-ajax/');
			
			var data = {
				url : ajax_url,
				direction : 'left'
			};
			
			load_project(data);
			
			return false;
			
		}).hover(function(){
				
			$(this).find('.outer').animate({
				marginTop : '-32px',
				marginLeft : '-32px',
				width : '65px',
				height : '65px'
			}, 250);
			
		}, function(){
			
			$(this).find('.outer').animate({
				marginTop : '-35px',
				marginLeft : '-35px',
				width : '71px',
				height : '71px'
			}, 250);
			
		});
		
		
		$(window).scroll(function(){
			
			if(!active) return false;
			
			window_y = $(window).scrollTop();
			var folio_y = (folio_single.offset().top  + folio_single.height());
			
			// folio_nav
			if(window_y > (folio_single.position().top - 50)) // -50 is the height of nav
			{
			
				folio_nav.css({'position' : 'fixed', 'top' : '50px'});
			}
			else
			{
				folio_nav.css({'position' : 'absolute', 'top' : '0px'});
			}
			
			var nav_y = (folio_nav.position().top + folio_nav.outerHeight());
			
			if(nav_y > folio_y)
			{
				//alert('s');
				folio_nav.css({'position' : 'absolute', 'top' : (folio_single.height() - folio_nav.outerHeight()) + 'px'});
			}
			
			// content
			/*if(window_y > (folio_single.offset().top - 15))
			{
				ajax_response.find('.content').css({'position' : 'fixed', 'top' : '85px'});
			}
			else
			{
				ajax_response.find('.content').css({'position' : 'absolute', 'top' : '0px'});
			}
			
			var content_y = (ajax_response.find('.content').position().top + ajax_response.find('.content').outerHeight());
			
			if(content_y > folio_y)
			{
				//alert('s');
				ajax_response.find('.content').css({'position' : 'absolute', 'top' : (folio_single.height() - ajax_response.find('.content').outerHeight() - 35) + 'px'});
			}*/
			
		});
		
		
	}
	
	/*--------------------------------------------------------------------------------------------
	*	
	*   setup_clients
	*
	*--------------------------------------------------------------------------------------------*/	
	
	function setup_clients()
	{
		var clients = $('#clients, #awards');
		
		clients.find('.client a, .award a').hover(function(){
			$(this).find('img').animate({opacity : 0}, 250);
		}, function(){
			$(this).find('img').animate({opacity : 1}, 250);
		});
	}



	/*--------------------------------------------------------------------------------------------
	*	
	*    Setup Slider
	*
	*--------------------------------------------------------------------------------------------*/
	
	function setup_slider()
	{
		var div = $('#slider');
		var width = div.find('.mask').width();
		var i = 0;
		var i_max = div.find('.slide').length - 1;
		
		div.find('a#slider_next').click(function(){
			
			if(i >= i_max) return false;
			
			i++;

			div.find('.mask').animate({'scrollLeft' : width*i}, 500);
			
			return false;
		});
		
		div.find('a#slider_next, a#slider_prev').hover(function(){
				
			$(this).find('.outer').animate({
				marginTop : '-33px',
				marginLeft : '-33px',
				width : '66px',
				height : '66px'
			}, 250);
			
		}, function(){
			
			$(this).find('.outer').animate({
				marginTop : '-36px',
				marginLeft : '-36px',
				width : '72px',
				height : '72px'
			}, 250);
			
		});
		
		
		div.find('a#slider_prev').click(function(){
			
			if(i <= 0) return false;
			
			i--;

			div.find('.mask').animate({'scrollLeft' : width*i}, 500);
			
			return false;
		})
		
		// reset slider
		div.find('.mask').animate({'scrollLeft' : 0}, 0);

	};
	
	
	/*--------------------------------------------------------------------------------------------
	*	
	*   setup_get_started
	*
	*--------------------------------------------------------------------------------------------*/	
	
	function setup_get_started()
	{
		var get_started = $('#get_started');
		
		get_started.find('a.download').each(function(){

			$(this).hover(function(){
				
				$(this).find('.inner').animate({
					marginLeft : '-40px',
					marginTop : '-40px',
					width : '80px',
					height : '80px'
				}, 250);
				
			}, function(){
				
				$(this).find('.inner').animate({
					marginLeft : '-35px',
					marginTop : '-35px',
					width : '70px',
					height : '70px'
				}, 250);
				
			});
			
		});

	}
	
	
	/*--------------------------------------------------------------------------------------------
	*	
	*   setup_footer
	*
	*--------------------------------------------------------------------------------------------*/	
	
	function setup_footer()
	{
		var footer = $('#footer');
		
		footer.find('.circle_button').each(function(){
					
			$(this).hover(function(){
				
				$(this).find('.outer').animate({
					marginLeft : '-30px',
					marginTop : '-30px',
					width : '60px',
					height : '60px'
				}, 250);
				
			}, function(){
				
				$(this).find('.outer').animate({
					marginLeft : '-25px',
					marginTop : '-25px',
					width : '50px',
					height : '50px'
				}, 250);
				
			});
			
		});
		
		// ajax subscribe
		$('#subForm').submit(function (e) {
	        e.preventDefault();
	        $(this).animate({opacity : 0.5}, 300);
	        $.getJSON(
	        this.action + "?callback=?",
	        $(this).serialize(),
	        function (data) {
	            if (data.Status === 400) {
	           		alert(data.Message);
	               	$('#subForm').animate({opacity : 1}, 300);
	            } else { // 200
	            	$('#subForm input[type="text"]').val('Thank you');
	            	$('#subForm').animate({opacity : 1}, 300);
	                //alert("Success: " + data.Message);
	            }
	        });
	    });
	

	}
	
	
	
	
	/*--------------------------------------------------------------------------------------------
	*	
	*    Document Ready
	*
	*--------------------------------------------------------------------------------------------*/

	$(document).ready(function()
	{
		
		//set_widths();
		setup_nav();
		setup_services();
		setup_folio();
		setup_clients();
		setup_slider();
		setup_get_started();
		setup_footer();
		
	});
	
	
	/*--------------------------------------------------------------------------------------------
		Window load
	--------------------------------------------------------------------------------------------*/
	$(window).load(function()
	{
		
	});
	
/*
	window.onresize = function(event) {
		//set_widths();
	}

	function set_widths()
	{
		
		return false;
		
		$("#nav, #header, #footer, #header_top, #header .container").css({
			width: $(document).width()
		});

	}
	*/
	
})(jQuery);